<?php
namespace plugins\community\controller;

use app\common\lib\model_operate\Transit; //引入此类
use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;

/**
 * 
 */
class AdminIndexController extends PluginAdminBaseController
{
    private  $pageNum = "20";

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////          社区分类       ////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 社区分类列表
     */
    public function index()
    {
        $data = $this->request->param();
        $where = 1;
        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $list = db('community_category')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){

            return $item;
        });
        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
        return $this->fetch('admin_index/category');
    }

    /**
     * 社区举报列表
     */
    public function report()
    {
        $data = $this->request->param();
        $where = 1;
        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $list = db('community_report')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){

            $item['addtime'] = date('Y-m-d H:i:s',$item['addtime']);
            $beUser = db('member')->where('uid',$item['community_id'])->find();//被举报的用户
            $user = db('member')->where('uid',$item['userid'])->find();//举报的用户
            $be_name = '';
            $name = '';
            if(!empty($beUser)){
                $be_name = $beUser['nick_name'];
            }
            if(!empty($user)){
                $name = $user['nick_name'];
            }
            $item['be_name'] = $be_name;
            $item['name'] = $name;
            return $item;
        });
        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    /**
     * 社区分类添加页面
     */
    public function addCategoryPage()
    {
        return $this->fetch();
    }
    /**
     * 添加社区分类
     */
    public function categoryAdd()
    {
        $param = $this->request->param();
        $add = $param['add'];
        $add['createtime'] = time();
        if(empty($add['title']))return api_json_echo(false,'标题为空！');
        $catname = db('community_category')->where('title', $add['title'])->find();
        if ($catname) {
            return api_json_echo(false, '该分类已存在！', null, 100);
        }
        $return = db('community_category')->insert($add);
        if ($return) {
            return api_json_echo(true, '添加成功！', null, 200);
        } else {
            return api_json_echo(false, '添加失败！', null, 101);
        }
    }
    /**
     * 社区分类编辑页面
     */
    public function editCategoryPage()
    {
        $param = $this->request->param();
        $data  = Db::name('community_category')->where('id', $param['id'])->find();
        $this->assign('data', $data);
        return $this->fetch();
    }
    /**
     * 编辑社区分类
     */
    public function editCategory()
    {
        $param = $this->request->post();
        $data = Db::name('community_category')->where('id', $param['id'])->setField($param);
        return api_json_echo(true, '编辑成功', null, 200);
    }
    /**
     * 社区分类排序
     */
    public function listorder()
    {
        $param = $this->request->param();
        $list  = array_combine($param['id'], $param['listorder']);
        if (isset($list)) {
            foreach ($list as $id => $listorder) {
                $save = db('community_category')->where('id', $id)->setField('sort', $listorder);
            }
            $this->success('排序成功！');
        } else {
            $this->error('排序失败！');
        }
    }
    /**
     * 删除社区分类
     */
    public function delCategory()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            return $this->error('请先选择要删除的分类！');
        }
        $res = Db::name('community_category')->where('id', $data['id'])->delete();
        if($res){
            return $this->success('删除成功');
        }
        return $this->error('删除失败');
    }



    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////          热门搜索       ////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 热门搜索列表
     */
    public function hotSearch()
    {
        $data = $this->request->param();
        $where = 1;
        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $list = db('hot_search')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){
            return $item;
        });
        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    /**
     * 热门搜索添加页面
     */
    public function addHotSearchPage()
    {
        return $this->fetch();
    }
    /**
     * 添加热门搜索
     */
    public function hotSearchAdd()
    {
        $param = $this->request->param();
        $add = $param['add'];
        $add['createtime'] = time();
        if(empty($add['title']))return api_json_echo(false,'标题为空！');
        $catname = db('hot_search')->where('title', $add['title'])->find();
        if ($catname) {
            return api_json_echo(false, '该热搜关键字已存在！', null, 100);
        }
        $return = db('hot_search')->insert($add);
        if ($return) {
            return api_json_echo(true, '添加成功！', null, 200);
        } else {
            return api_json_echo(false, '添加失败！', null, 101);
        }
    }
    /**
     * 热门搜索编辑页面
     */
    public function editHotSearchPage()
    {
        $param = $this->request->param();
        $data  = Db::name('hot_search')->where('id', $param['id'])->find();
        $this->assign('data', $data);
        return $this->fetch();
    }
    /**
     * 编辑热门搜索
     */
    public function editHotSearch()
    {
        $param = $this->request->post();
        $data = Db::name('hot_search')->where('id', $param['id'])->setField($param);
        return api_json_echo(true, '编辑成功', null, 200);
    }
    /**
     * 删除热门搜索
     */
    public function delHotSearch()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            return $this->error('请先选择要删除的热门搜索！');
        }
        $res = Db::name('hot_search')->where('id', $data['id'])->delete();
        if($res){
            return $this->success('删除成功');
        }
        return $this->error('删除失败');
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////          举报管理       ////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////











}